#include <linux/module.h>
#include <linux/platform_device.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/of.h>

#define send_bits 9

/* 1. 确定主设备号                                                                 */
static int major = 0;
static struct class * vernon_spi_class;

static struct gpio_desc * gpio_pin_sda;
static struct gpio_desc * gpio_pin_scl;
static struct gpio_desc * gpio_pin_cs;

/* 3. 实现对应的open/read/write等函数，填入file_operations结构                 */
static ssize_t vernon_spi_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk("[vernon spi:]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* write(fd, &val, 1); */
static ssize_t vernon_spi_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	u_int16_t store_buf;
	int i;
	//struct inode *inode = file_inode(file);
	//int minor = iminor(inode);
	// printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(&store_buf, buf, size);
	// printk("[vernon spi]store_buf: %x , size %u\n", store_buf, size);

	/* 根据次设备号和status控制gpio */
	// start to trans
	gpiod_set_value(gpio_pin_cs, 1); //low level valid, so set the 1 to make level low(be set in the device tree)

	for(i = 0; i < send_bits; i++)
	{
		gpiod_set_value(gpio_pin_scl, 0);
		gpiod_set_value(gpio_pin_sda, (store_buf & (0x0100 >> i))); //big endian
		gpiod_set_value(gpio_pin_scl, 1);
	}

	//end transmission
	gpiod_set_value(gpio_pin_scl, 0);
	gpiod_set_value(gpio_pin_cs, 0);

	return 1;
}

static int vernon_spi_open (struct inode *node, struct file *file)
{
	//int minor = iminor(node);
	
	printk("[vernon spi]%s %s lRDWRine %d\n", __FILE__, __FUNCTION__, __LINE__);
	/* 根据次设备号初始化gpio */
	gpiod_direction_output(gpio_pin_sda, 0);
	gpiod_direction_output(gpio_pin_scl, 0);
	gpiod_direction_output(gpio_pin_cs, 0);

	gpiod_set_value(gpio_pin_cs, 0); // release cs line
	
	return 0;
}

static int vernon_spi_close (struct inode *node, struct file *file)
{
	printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 定义自己的file_operations结构 */
static struct file_operations vernon_spi_drv = {
	.owner	 = THIS_MODULE,
	.open    = vernon_spi_open,
	.read    = vernon_spi_read,
	.write   = vernon_spi_write,
	.release = vernon_spi_close,
};


// =========================== device tree section ===========================
/* 4. 从platform_device获得GPIO
 *    把file_operations结构体告诉内核：注册驱动程序
 */
static int vernon_spi_probe(struct platform_device *pdev)
{
	//int err;
	
	printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

	/* 4.1 设备树中定义�? xx-gpios=<...>;	*/
    gpio_pin_sda = gpiod_get(&pdev -> dev, "sda", 0);
	gpio_pin_scl = gpiod_get(&pdev -> dev, "scl", 0);
	gpio_pin_cs  = gpiod_get(&pdev -> dev, "cs", 0);
	if (IS_ERR(gpio_pin_sda)) {
		dev_err(&pdev -> dev, "Failed to get SDA GPIO name\n");
		return PTR_ERR(gpio_pin_sda);
	}else if (IS_ERR(gpio_pin_scl)) {
		dev_err(&pdev -> dev, "Failed to get SCL GPIO name\n");
		return PTR_ERR(gpio_pin_scl);/* 4.2 注册file_operations 	*/
	}else if (IS_ERR(gpio_pin_cs)) {
		dev_err(&pdev -> dev, "Failed to get CS GPIO name\n");
		return PTR_ERR(gpio_pin_cs);
	}
    
	/* 4.2 注册file_operations 	*/
	major = register_chrdev(0, "vernon_spi", &vernon_spi_drv);  /* /dev/spi0 */

	vernon_spi_class = class_create(THIS_MODULE, "verno/* 4.2 注册file_operations 	*/n_spi_class");
	if (IS_ERR(vernon_spi_class)) {
		printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "vernon_spi");
		gpiod_put(gpio_pin_sda);
		gpiod_put(gpio_pin_scl);
		gpiod_put(gpio_pin_cs);
		return PTR_ERR(vernon_spi_class);
	}

	device_create(vernon_spi_class, NULL, MKDEV(major, 0), NULL, "vernon_spi%d", 0); /* /dev/vernon_spi0 */
	/* 4. 从platform_device获得GPIO
 	 *    把file_operations结构体告诉内核：注册驱动程序
 	 */
    return 0;
    
}

static int vernon_spi_remove(struct platform_device *pdev)
{
	device_destroy(vernon_spi_class, MKDEV(major, 0));
	class_destroy(vernon_spi_class);
	unregister_chrdev(major, "vernon_spi");
	gpiod_put(gpio_pin_sda);
	gpiod_put(gpio_pin_scl);
	gpiod_put(gpio_pin_cs);
    
    return 0;
}

static const struct of_device_id vernon_spi_driver_device[] = {
    { .compatible = "vernon,spi_9bits" },
    { },
};


/* 1. 定义platform_driver */
static struct platform_driver vernon_spi_driver = {
    .probe      = vernon_spi_probe,
    .remove     = vernon_spi_remove,
    .driver     = {
        .name   = "vernon_spi",
        .of_match_table = vernon_spi_driver_device,
    },
};

/* 2. 在入口函数注册platform_driver */
static int __init vernon_spi_init(void)
{
    int err;
    
	printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	
    err = platform_driver_register(&vernon_spi_driver); 
	
	return err;
}

/* 3. 有入口函数就应该有出口函数：卸载驱动程序时，就会去调用这个出口函�? *     卸载platform_driver
 */
static void __exit vernon_spi_exit(void)
{
	printk("[vernon spi]%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    platform_driver_unregister(&vernon_spi_driver);
}


/* 7. 其他完善：提供设备信息，自动创建设备节点                                     */

module_init(vernon_spi_init);
module_exit(vernon_spi_exit);

MODULE_LICENSE("GPL");
